import * as types from './mutation-types'
import Vue from 'vue'
export default {
  // 获取所有在线咨询节点
  [types.SET_ALL_WEBCHAT_SCHDULE_NODE_LIST] (state, data) {
    Vue.set(state, 'webchatSchNodeList', data)
  },
  //  刷新各类型下的节点
  [types.RESET_WEBCHAT_SCHDULE_LIST_NODE] (state, {node, data}) {
    Vue.set(state, node, data)
  },
  //  保存修改节点
  [types.UPDATE_WEBCHAT_SCHDULE_LIST_NODE] (state, {node, data}) {
    let indexOne = -1
    let indexTwo = -1
    state.webchatSchNodeList.forEach((item, index) => {
      if (item._id === data._id) {
        indexOne = index
      }
    })
    state[node].forEach((obj, index) => {
      if (obj._id === data._id) {
        indexTwo = index
      }
    })
    Vue.set(state.webchatSchNodeList, [indexOne], data)
    Vue.set(state[node], [indexTwo], data)
  },
  // 新增节点
  [types.ADD_WEBCHAT_SCHDULE_LIST_NODE] (state, {node, data}) {
    state.webchatSchNodeList.push(data)
    state[node].push(data)
  },
  //  搜索节点
  [types.SEARCH_WEBCHAT_SCHDULE_LIST_NODE] (state, {type, node, data}) {
    let arr = []
    state.webchatSchNodeList.forEach((item) => {
      if (item.type === type) {
        if (data) {
          if (item.name.indexOf(data) > -1) {
            arr.push(item)
          }
        } else {
          arr.push(item)
        }
      }
    })
    Vue.set(state, node, arr)
  },
//  删除节点
  [types.DELETE_WEBCHAT_SCHDULE_LIST_NODE] (state, {node, _id}) {
    let indexOne = -1
    let indexTwo = -1
    state.webchatSchNodeList.forEach((item, index) => {
      if (item._id === _id) {
        indexOne === index
      }
    })
    state[node].forEach((obj, index) => {
      if (obj._id === _id) {
        indexTwo === index
      }
    })
    if (indexOne >= 0) {
      state.webchatSchNodeList.splice(indexOne, 1)
    }
    if (indexTwo >= 0) {
      state[node].splice(indexTwo, 1)
    }
  },
  //  设置当前编辑的内容
  [types.SET_CURRENT_PROCESS_EDIT] (state, {data}) {
    Vue.set(state, 'currentEditProcess', data)
  },
  //  设置所有的流程
  [types.SET_ALL_WEBCHAT_SCHDULE_PROCESS_LIST] (state, data) {
    Vue.set(state, 'webchatProcessLists', data)
  },
  //  点击节点生效按钮
  [types.SET_RELOAD_NODE] (state, data) {
    Vue.set(state, 'reloadNode', data)
  },
  //  点击流程生效按钮
  [types.SET_RELOAD_PROCESS] (state, data) {
    Vue.set(state, 'reloadProcessRandom', data)
  },
  [types.SET_PROCESS_EDIT_NODE_RANDOM] (state, data) {
    Vue.set(state, 'processEditNode', data)
  },
  //  搜索流程
  [types.SEARCH_WEBCHAT_SCHDULE_LIST_PROCESS] (state, data) {
    let arr = []
    if (!data) {
      arr = state.webchatProcessLists
    } else {
      state.webchatProcessLists.forEach((item) => {
        if (data) {
          if (item.name.indexOf(data) > -1) {
            arr.push(item)
          }
        }
      })
    }
    Vue.set(state, 'currentProcessLists', arr)
  }
}
